--    Author    : MAYANTING
--    Name      : ADM.RPT_SUST_DLMG_TERM_PRO_LOAB.HQL
--    Functions : 表1：贷款余额产品期限结构表
--    Purpose   :
--    Revisions or Comments
--    VER        DATE        AUTHOR           DESCRIPTION
--   ---------  ----------  ---------------  ------------------------------------
--    1.0        2018-06-11  MAYANTING           1.CREATE THE PROCEDURE
--
INSERT OVERWRITE TABLE ADM.RPT_SUST_DLMG_TERM_PRO_LOAB PARTITION (DATA_DATE = '20180331')
SELECT  AREA.AREA_CODE_4                AS FIN_ORG_DIST
       ,T.CCY
       ,T.BUSINESS_TYPE
       ,T.BUSINESS_DESC
       ,SUM(T.BAL_0_6M    )/100000000        --6个月（含）以内-贷款余额
       ,SUM(T.WSUM_0_6M   )/100000000        --6个月（含）以内-加权金额
       ,SUM(T.BAL_6_12M   )/100000000        --6个月至1年（含）-贷款余额
       ,SUM(T.WSUM_6_12M  )/100000000        --6个月至1年（含）-加权金额
       ,SUM(T.BAL_1_3Y    )/100000000        --1至3年（含）-贷款余额
       ,SUM(T.WSUM_1_3Y   )/100000000        --1至3年（含）-加权金额
       ,SUM(T.BAL_3_5Y    )/100000000        --3至5年（含）-贷款余额
       ,SUM(T.WSUM_3_5Y   )/100000000        --3至5年（含）-加权金额
       ,SUM(T.BAL_5_10Y   )/100000000        --5至10年（含）-贷款余额
       ,SUM(T.WSUM_5_10Y  )/100000000        --5至10年（含）-加权金额
       ,SUM(T.BAL_10_999Y )/100000000        --10年以上-贷款余额
       ,SUM(T.WSUM_10_999Y)/100000000        --10年以上-加权金额
       ,SUM(T.BAL_ALL     )/100000000        --所有期限-贷款余额
       ,SUM(T.WSUM_ALL    )/100000000        --所有期限-加权金额
FROM (
    -- 个人+机构
    SELECT
     YE.FIN_ORG_NO                                                            AS FIN_ORG_NO
    ,YE.CCY                                                                   AS CCY
    ,YE.CLIENT_TYPE                                                           AS BUSINESS_TYPE
    ,CASE WHEN YE.CLIENT_TYPE='1' THEN '个人'
          WHEN YE.CLIENT_TYPE='0' THEN '机构' END                             AS BUSINESS_DESC
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_0_6M            --6个月（含）以内-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_0_6M           --6个月（含）以内-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_6_12M           --6个月至1年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_6_12M          --6个月至1年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_1_3Y            --1至3年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_1_3Y           --1至3年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_3_5Y            --3至5年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_3_5Y           --3至5年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_5_10Y           --5至10年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_5_10Y          --5至10年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_10_999Y         --10年以上-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_10_999Y        --10年以上-加权金额
    ,COALESCE(YE.ACTUAL_BAL,0)                                                AS BAL_ALL             --所有期限-贷款余额
    ,COALESCE(YE.WSUM_BAL,0)                                                  AS WSUM_ALL            --所有期限-加权金额
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE DATA_DATE='20180331') YE

    UNION ALL

    -- 个人_普通贷款
    SELECT
     YE.FIN_ORG_NO                                                            AS FIN_ORG_NO
    ,YE.CCY                                                                   AS CCY
    ,'11'                                                                     AS BUSINESS_TYPE
    ,'普通贷款'                                                               AS BUSINESS_DESC
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_0_6M            --6个月（含）以内-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_0_6M           --6个月（含）以内-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_6_12M           --6个月至1年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_6_12M          --6个月至1年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_1_3Y            --1至3年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_1_3Y           --1至3年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_3_5Y            --3至5年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_3_5Y           --3至5年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_5_10Y           --5至10年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_5_10Y          --5至10年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_10_999Y         --10年以上-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_10_999Y        --10年以上-加权金额
    ,COALESCE(YE.ACTUAL_BAL,0)                                                AS BAL_ALL             --所有期限-贷款余额
    ,COALESCE(YE.WSUM_BAL,0)                                                  AS WSUM_ALL            --所有期限-加权金额
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE CLIENT_TYPE='1' AND BUSINESS_TYPE IN ('F0211','F0212','F02131','F02132','F0219','F02201','F02202')AND DATA_DATE='20180331') YE

UNION ALL

    -- 个人_消费贷款
    SELECT
     YE.FIN_ORG_NO                                                            AS FIN_ORG_NO
    ,YE.CCY                                                                   AS CCY
    ,'111'                                                                    AS BUSINESS_TYPE
    ,'消费贷款'                                                               AS BUSINESS_DESC
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_0_6M            --6个月（含）以内-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_0_6M           --6个月（含）以内-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_6_12M           --6个月至1年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_6_12M          --6个月至1年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_1_3Y            --1至3年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_1_3Y           --1至3年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_3_5Y            --3至5年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_3_5Y           --3至5年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_5_10Y           --5至10年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_5_10Y          --5至10年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_10_999Y         --10年以上-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_10_999Y        --10年以上-加权金额
    ,COALESCE(YE.ACTUAL_BAL,0)                                                AS BAL_ALL             --所有期限-贷款余额
    ,COALESCE(YE.WSUM_BAL,0)                                                  AS WSUM_ALL            --所有期限-加权金额
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE CLIENT_TYPE='1' AND BUSINESS_TYPE IN ('F0211','F0212','F02131','F02132','F0219')AND DATA_DATE='20180331') YE

    UNION ALL

    -- 个人_个人住房贷款+个人汽车消费贷款+其他消费贷款+经营贷款
    SELECT
     YE.FIN_ORG_NO                                                            AS FIN_ORG_NO
    ,YE.CCY                                                                   AS CCY
    ,CASE WHEN YE.BUSINESS_TYPE='F0211' THEN '1111' -- 个人住房贷款
          WHEN YE.BUSINESS_TYPE='F0212' THEN '1112' -- 个人汽车消费贷款
          WHEN YE.BUSINESS_TYPE='F0219' THEN '1114' -- 其他消费贷款
          WHEN YE.BUSINESS_TYPE IN ('F02201','F02202') THEN '112'  -- 经营贷款
     END                                                                      AS BUSINESS_TYPE
    ,CASE WHEN YE.BUSINESS_TYPE='F0211' THEN '个人住房贷款'
          WHEN YE.BUSINESS_TYPE='F0212' THEN '个人汽车消费贷款'
          WHEN YE.BUSINESS_TYPE='F0219' THEN '其他消费贷款'
          WHEN YE.BUSINESS_TYPE IN ('F02201','F02202') THEN '经营贷款'
     END                                                                      AS BUSINESS_DESC
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_0_6M            --6个月（含）以内-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_0_6M           --6个月（含）以内-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_6_12M           --6个月至1年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_6_12M          --6个月至1年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_1_3Y            --1至3年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_1_3Y           --1至3年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_3_5Y            --3至5年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_3_5Y           --3至5年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_5_10Y           --5至10年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_5_10Y          --5至10年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_10_999Y         --10年以上-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_10_999Y        --10年以上-加权金额
    ,COALESCE(YE.ACTUAL_BAL,0)                                                AS BAL_ALL             --所有期限-贷款余额
    ,COALESCE(YE.WSUM_BAL,0)                                                  AS WSUM_ALL            --所有期限-加权金额
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE CLIENT_TYPE='1' AND BUSINESS_TYPE IN ('F0211','F0212','F02201','F02202','F0219')AND DATA_DATE='20180331') YE

    UNION ALL

    -- 个人_助学贷款
    SELECT
     YE.FIN_ORG_NO                                                            AS FIN_ORG_NO
    ,YE.CCY                                                                   AS CCY
    ,'1113'                                                                   AS BUSINESS_TYPE
    ,'助学贷款'                                                               AS BUSINESS_DESC
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_0_6M            --6个月（含）以内-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_0_6M           --6个月（含）以内-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_6_12M           --6个月至1年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_6_12M          --6个月至1年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_1_3Y            --1至3年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_1_3Y           --1至3年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_3_5Y            --3至5年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_3_5Y           --3至5年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_5_10Y           --5至10年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_5_10Y          --5至10年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_10_999Y         --10年以上-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_10_999Y        --10年以上-加权金额
    ,COALESCE(YE.ACTUAL_BAL,0)                                                AS BAL_ALL             --所有期限-贷款余额
    ,COALESCE(YE.WSUM_BAL,0)                                                  AS WSUM_ALL            --所有期限-加权金额
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE CLIENT_TYPE='1' AND BUSINESS_TYPE IN ('F02131','F02132')AND DATA_DATE='20180331') YE

UNION ALL

    -- 机构_普通贷款
    SELECT
     YE.FIN_ORG_NO                                                            AS FIN_ORG_NO
    ,YE.CCY                                                                   AS CCY
    ,'21'                                                                     AS BUSINESS_TYPE
    ,'普通贷款'                                                               AS BUSINESS_DESC
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_0_6M            --6个月（含）以内-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_0_6M           --6个月（含）以内-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_6_12M           --6个月至1年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_6_12M          --6个月至1年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_1_3Y            --1至3年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_1_3Y           --1至3年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_3_5Y            --3至5年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_3_5Y           --3至5年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_5_10Y           --5至10年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_5_10Y          --5至10年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_10_999Y         --10年以上-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_10_999Y        --10年以上-加权金额
    ,COALESCE(YE.ACTUAL_BAL,0)                                                AS BAL_ALL             --所有期限-贷款余额
    ,COALESCE(YE.WSUM_BAL,0)                                                  AS WSUM_ALL            --所有期限-加权金额
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE CLIENT_TYPE='0' AND BUSINESS_TYPE IN ('F02203','F023')AND DATA_DATE='20180331') YE

    UNION ALL

    -- 机构_贸易融资
    SELECT
     YE.FIN_ORG_NO                                                            AS FIN_ORG_NO
    ,YE.CCY                                                                   AS CCY
    ,'22'                                                                     AS BUSINESS_TYPE
    ,'贸易融资'                                                               AS BUSINESS_DESC
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_0_6M            --6个月（含）以内-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_0_6M           --6个月（含）以内-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_6_12M           --6个月至1年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_6_12M          --6个月至1年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_1_3Y            --1至3年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_1_3Y           --1至3年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_3_5Y            --3至5年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_3_5Y           --3至5年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_5_10Y           --5至10年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_5_10Y          --5至10年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_10_999Y         --10年以上-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_10_999Y        --10年以上-加权金额
    ,COALESCE(YE.ACTUAL_BAL,0)                                                AS BAL_ALL             --所有期限-贷款余额
    ,COALESCE(YE.WSUM_BAL,0)                                                  AS WSUM_ALL            --所有期限-加权金额
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE CLIENT_TYPE='0' AND BUSINESS_TYPE IN ('F081','F082')AND DATA_DATE='20180331') YE

    UNION ALL

    -- 机构_经营贷款+固定资产贷款+融资租赁+转贷款+并购贷款
    SELECT
     YE.FIN_ORG_NO                                                            AS FIN_ORG_NO
    ,YE.CCY                                                                   AS CCY
    ,CASE WHEN YE.BUSINESS_TYPE='F02203' THEN '211'
          WHEN YE.BUSINESS_TYPE='F023'   THEN '212'
          WHEN YE.BUSINESS_TYPE='F09'    THEN '23'
          WHEN YE.BUSINESS_TYPE='F11'    THEN '24'
          WHEN YE.BUSINESS_TYPE='F12'    THEN '25'
     END                                                                      AS BUSINESS_TYPE
    ,CASE WHEN YE.BUSINESS_TYPE='F02203' THEN '经营贷款'
          WHEN YE.BUSINESS_TYPE='F023'   THEN '固定资产贷款'
          WHEN YE.BUSINESS_TYPE='F09'    THEN '融资租赁'
          WHEN YE.BUSINESS_TYPE='F11'    THEN '转贷款'
          WHEN YE.BUSINESS_TYPE='F12'    THEN '并购贷款'
     END                                                                      AS BUSINESS_DESC
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_0_6M            --6个月（含）以内-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='1' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_0_6M           --6个月（含）以内-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_6_12M           --6个月至1年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='2' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_6_12M          --6个月至1年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_1_3Y            --1至3年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='3' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_1_3Y           --1至3年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_3_5Y            --3至5年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='4' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_3_5Y           --3至5年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_5_10Y           --5至10年（含）-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='5' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_5_10Y          --5至10年（含）-加权金额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.ACTUAL_BAL ELSE 0 END,0)    AS BAL_10_999Y         --10年以上-贷款余额
    ,COALESCE(CASE WHEN YE.TERM_CODE ='6' THEN YE.WSUM_BAL   ELSE 0 END,0)    AS WSUM_10_999Y        --10年以上-加权金额
    ,COALESCE(YE.ACTUAL_BAL,0)                                                AS BAL_ALL             --所有期限-贷款余额
    ,COALESCE(YE.WSUM_BAL,0)                                                  AS WSUM_ALL            --所有期限-加权金额
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE CLIENT_TYPE='0' AND BUSINESS_TYPE IN ('F02203','F023','F09','F11','F12')AND DATA_DATE='20180331') YE

    UNION ALL

    -- 合计
    SELECT
     LOAB.FIN_ORG_NO                                                              AS FIN_ORG_NO
    ,LOAB.CCY                                                                     AS CCY
    ,'3'                                                                          AS BUSINESS_TYPE
    ,'合计'                                                                       AS BUSINESS_DESC
    ,COALESCE(CASE WHEN LOAB.TERM_CODE ='1' THEN LOAB.ACTUAL_BAL ELSE 0 END,0)    AS BAL_0_6M            --6个月（含）以内-贷款余额
    ,COALESCE(CASE WHEN LOAB.TERM_CODE ='1' THEN LOAB.WSUM_BAL   ELSE 0 END,0)    AS WSUM_0_6M           --6个月（含）以内-加权金额
    ,COALESCE(CASE WHEN LOAB.TERM_CODE ='2' THEN LOAB.ACTUAL_BAL ELSE 0 END,0)    AS BAL_6_12M           --6个月至1年（含）-贷款余额
    ,COALESCE(CASE WHEN LOAB.TERM_CODE ='2' THEN LOAB.WSUM_BAL   ELSE 0 END,0)    AS WSUM_6_12M          --6个月至1年（含）-加权金额
    ,COALESCE(CASE WHEN LOAB.TERM_CODE ='3' THEN LOAB.ACTUAL_BAL ELSE 0 END,0)    AS BAL_1_3Y            --1至3年（含）-贷款余额
    ,COALESCE(CASE WHEN LOAB.TERM_CODE ='3' THEN LOAB.WSUM_BAL   ELSE 0 END,0)    AS WSUM_1_3Y           --1至3年（含）-加权金额
    ,COALESCE(CASE WHEN LOAB.TERM_CODE ='4' THEN LOAB.ACTUAL_BAL ELSE 0 END,0)    AS BAL_3_5Y            --3至5年（含）-贷款余额
    ,COALESCE(CASE WHEN LOAB.TERM_CODE ='4' THEN LOAB.WSUM_BAL   ELSE 0 END,0)    AS WSUM_3_5Y           --3至5年（含）-加权金额
    ,COALESCE(CASE WHEN LOAB.TERM_CODE ='5' THEN LOAB.ACTUAL_BAL ELSE 0 END,0)    AS BAL_5_10Y           --5至10年（含）-贷款余额
    ,COALESCE(CASE WHEN LOAB.TERM_CODE ='5' THEN LOAB.WSUM_BAL   ELSE 0 END,0)    AS WSUM_5_10Y          --5至10年（含）-加权金额
    ,COALESCE(CASE WHEN LOAB.TERM_CODE ='6' THEN LOAB.ACTUAL_BAL ELSE 0 END,0)    AS BAL_10_999Y         --10年以上-贷款余额
    ,COALESCE(CASE WHEN LOAB.TERM_CODE ='6' THEN LOAB.WSUM_BAL   ELSE 0 END,0)    AS WSUM_10_999Y        --10年以上-加权金额
    ,COALESCE(LOAB.ACTUAL_BAL,0)                                                  AS BAL_ALL             --所有期限-贷款余额
    ,COALESCE(LOAB.WSUM_BAL,0)                                                    AS WSUM_ALL            --所有期限-加权金额
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE DATA_DATE='20180331') LOAB
   )T
   -- 与地区表最细级关联
  LEFT JOIN (SELECT * FROM DIMENSION.T_ORG_AREA_RELATION WHERE AREA_LEVEL='3') REAL ON T.FIN_ORG_NO=REAL.ORG_ID AND '20180331' BETWEEN REAL.START_DATE AND REAL.END_DATE
  LEFT JOIN DIMENSION.T_AREA_CODE AREA ON REAL.AREA_CODE=AREA.AREA_CODE_4 AND '20180331' BETWEEN AREA.START_DATE AND AREA.END_DATE
  GROUP BY   AREA.AREA_CODE_4
            ,T.CCY
            ,T.BUSINESS_TYPE
            ,T.BUSINESS_DESC
;